package io.jnet.client.clientchannelcontext;

import java.nio.channels.AsynchronousSocketChannel;
import com.jfireframework.baseutil.collection.buffer.ByteBuf;
import io.jnet.common.api.impl.BaseChannelContext;
import io.jnet.common.bufstorage.BufStorage;
import io.jnet.common.decodec.FrameDecodec;
import io.jnet.common.streamprocessor.ProcesserUtil;
import io.jnet.common.streamprocessor.StreamProcessor;
import io.jnet.common.util.AioListener;

public abstract class AbstractClientChannelContext extends BaseChannelContext
{
    protected final StreamProcessor[] outProcessors;
    
    public AbstractClientChannelContext(//
            int maxMerge, //
            BufStorage bufStorage, //
            StreamProcessor[] outProcessors, //
            AsynchronousSocketChannel socketChannel, //
            AioListener aioListener, //
            StreamProcessor[] inProcessors, //
            FrameDecodec frameDecodec)
    {
        super(bufStorage, maxMerge, aioListener, inProcessors, socketChannel, frameDecodec);
        this.outProcessors = outProcessors;
    }
    
    @Override
    public void push(Object send, int index) throws Throwable
    {
        send = ProcesserUtil.process(this, outProcessors, send, index);
        if (send instanceof ByteBuf<?>)
        {
            bufStorage.putBuf((ByteBuf<?>) send);
            writeHandler.registerWrite();
        }
    }
}
